'use strict';

import { merchant_disheModel } from "../model/merchant_dishe";
import { ServiceBase, Exceptions } from '../kalend';
import config from "../middleware/config";
var moment = require('moment');

class Merchant_disheService extends ServiceBase {
    constructor() {
        super(merchant_disheModel)
    }
    async query(params: any) {
        const offset = params.offset || 1;
        const limit = params.limit || 15;
        const mid = params.mid;
        const status = params.status;
        const name = params.name;

        let filter: any = {};
        if (mid) {
            filter.merchantid = mid;
        }
        if (status === 0 || status === 1) {
            filter.status = status;
        }
        if (name) {
            filter.name = { $regex: name, $options: 'i' };
        }
        let data = await super.queryPagination(offset, limit, {
            filter: filter,
            select: 'name lable type price status order remarks updateTime merchantid',
            populateOptions: {
                path: 'merchantid',
                select: 'name'
            }
        });
        data.rows = data.rows.map((item) => {
            if (item.updateTime) {
                item.updateTime = moment(item.updateTime).format('YYYY-MM-DD HH:mm');
            }
            if (item.status == '0') {
                item.status = '已下架';
            } else if (item.status == '1') {
                item.status = '售卖中';
            }
            if (item.merchantid) {
                item.mername = item.merchantid.name;
            }
            return item;
        });
        return data;
    }
    async queryall(mid?: any) {
        return this.queryAll({
            filter: { merchantid: mid, status: 1 },
            select: 'name lable type price img',
            sort: 'type order'
        });
    }

    async querypage(params: any) {
        const offset = params.offset || 1;
        const limit = params.limit || 15;
        const mid = params.mid;
        return this.queryPagination(offset, limit, {
            filter: { merchantid: mid, status: 1 },
            select: 'name lable type price img',
            sort: 'type order'
        });
    }

    async save(doc: any) {
        if (!doc.merchantid) {
            throw Exceptions.known('商户不能为空');
        }
        if (!doc.name) {
            throw Exceptions.known('菜品名不能为空');
        }
        if (!doc.lable) {
            throw Exceptions.known('菜品描述不能为空');
        }
        if (!doc.type) {
            throw Exceptions.known('菜品类型不能为空');
        }
        if (!doc.price) {
            throw Exceptions.known('菜品价格不能为空');
        }
        if (!doc.img) {
            throw Exceptions.known('菜品图片不能为空');
        }
        if (!doc.creater) {
            throw Exceptions.known('创建人不能为空');
        }
        const apiurl = config.get("apiforarea")["imgurl"];
        if (doc.img) {
            if (doc.img.indexOf('http') < 0) {
                doc.img = apiurl + doc.img;
            }
        }
        if (doc._id) {
            return this.update(doc._id, doc);
        } else {
            return this.create(doc);
        }
    }

    async lock(userid: any) {
        return this.update(userid, { status: 0 });
    }
    async open(userid: any) {
        return this.update(userid, { status: 1 });
    }
}

export const merchant_disheService = new Merchant_disheService();